package ij.umasuo.algorithms;

import ij.ImagePlus;
import ij.plugin.filter.Analyzer;
import ij.umasuo.MyConstants;
import ij.umasuo.AOTFController.CtrInterface;
import ij.umasuo.AOTFController.TestCtr;
import ij.umasuo.algorithms.QUICKPALM.QuickPalmProcessor;
import ij.umasuo.algorithms.gaussian.GaussianProcessor;
import ij.umasuo.gui.working.ReconstructionViewer;

/**
 * organize the algorithms that we need to process the image .
 * @author liuquan
 * @date 2013-08-19
 * @version 1.0
 *
 */
public class AlgorithmManager {
	
	private static AlgorithmManager instance = null;
	private int sourceModel = 0;//the model of the image source, we got two type:offline & online 0 means offline, and 1 means online  
	private CtrInterface onlineImgSource = null;
	private boolean isProcess = true;//here we use this to stop the process
	private AlgorithmManager(){}

	public void startProcess(){
		if(MyConstants.CUR_ALGORITHM == MyConstants.CENTER_OF_MASS){
			startProcessStorm();
		}else if(MyConstants.CUR_ALGORITHM == MyConstants.GAUSSIAN_FITTING){
			startProcessGaussian();
		}
	}
	/**
	 * start process the images,here we use the method in quickpalm to process the images
	 */
	public void startProcessStorm(){
		if(sourceModel == MyConstants.ALGORITHM_ONLINE){
			isProcess = true;
			PorcessThread processor = new PorcessThread();
			processor.start();
		}
	}
	/**
	 *  process the images with the gaussian fitting method..
	 */
	public void startProcessGaussian(){
		if(sourceModel == MyConstants.ALGORITHM_ONLINE){
			isProcess = true;
			PorcessThread processor = new PorcessThread();
			processor.start();
		}
	}
	
	public void stopProcess(){
		isProcess = false;
	}
	/**
	 * stop the process of storm
	 */
	public void stopProcessStorm(){
		isProcess = false;
	}
	public void	stopProcessGaussian(){
		isProcess = false;
	}
	public static AlgorithmManager getInstance(){
		if(instance == null){
			instance = new AlgorithmManager();
		}
		return instance;
	}

	public void setSourceModel(int model){
		this.sourceModel = model;
	}
	public void setOnlineImgSrouce(CtrInterface source){
		this.onlineImgSource = source;
	}
	
	//this should move out!!
	public class PorcessThread extends Thread{
		@Override
		public void run() {
			if(MyConstants.CUR_ALGORITHM == MyConstants.CENTER_OF_MASS){
				Analyzer.getResultsTable().reset();//RESET the particle table
				QuickPalmProcessor processor = new QuickPalmProcessor();
				if(processor.init()){
					MyConstants.isCanTakingDate = true;
					while(isProcess){
						ImagePlus imp = onlineImgSource.getNextImage();
	//					ImagePlus imp = DStormCtr.getNextImage1();
						int channel = onlineImgSource.getNextChannel();
						int frame = onlineImgSource.getNextFrame();
						if(imp != null){
							processor.process(imp, channel, frame);//only the image is not null can we process it
	//						processor.process(imp, 0, 0);//only the image is not null can we process it
						}
					}
					processor.showResult();//here we stop the process
					MyConstants.isCanTakingDate = false;
				}
			}else if(MyConstants.CUR_ALGORITHM == MyConstants.GAUSSIAN_FITTING){
				GaussianProcessor processor = new GaussianProcessor();
				int is3d = MyConstants.IS_3D?3:2;
//				System.out.println(is3d);
				Analyzer.getResultsTable().reset();//RESET the particle table
				ReconstructionViewer.getInstance().reset();
				MyConstants.isCanTakingDate = true;
//				onlineImgSource = new TestCtr();
				while(isProcess){
					String path = onlineImgSource.getNextImagePath();
					int channel = onlineImgSource.getNextChannel();
					int frame = onlineImgSource.getNextFrame();
					if(path != null){
						processor.process(is3d,path, channel, frame);//only the image is not null can we process it
					}
				}
				processor.showResult();//here we stop the process
				
				MyConstants.isCanTakingDate = false;
			}
		}
	}
}
